package com.leonzx.base.config.ureport.reportprovider;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.leonzx.base.dao.system.UreportFileDAO;
import com.leonzx.base.entity.system.UreportFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/7/4 21:12
 */
@Component
public class DataBaseReportProvider implements ReportProvider {

    private static final String NAME = "sqlserver-provider";

    //前缀，ureport底层会调用 getPrefix 方法来获取报表操作的Provier类
    private String prefix = "sqlserver:";

    //是否禁用
    private boolean disabled;

    @Autowired
    private UreportFileDAO ureportFileDAO;

    @Override
    public InputStream loadReport(String file) {
        UreportFile ureportFile = ureportFileDAO.queryUreportFileEntityByName(getCorrectName(file));
        byte[] content = ureportFile.getContent().getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    @Override
    public void deleteReport(String file) {
        ureportFileDAO.deleteReportFileByName(getCorrectName(file));
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<UreportFile> list = ureportFileDAO.queryReportFileList();
        List<ReportFile> reportList = new ArrayList<>();
        for (UreportFile ureportFile : list) {
            reportList.add(new ReportFile(ureportFile.getName(), ureportFile.getUpdateTime()));
        }
        return reportList;
    }

    @Override
    public void saveReport(String file, String content) {
        file = getCorrectName(file);
        UreportFile ureportFile = ureportFileDAO.queryUreportFileEntityByName(file);
        Date currentDate = new Date();
        if (ureportFile == null) {
            ureportFile = new UreportFile();
            ureportFile.setName(file);
            ureportFile.setContent(content);
            ureportFile.setCreateTime(currentDate);
            ureportFile.setUpdateTime(currentDate);
            ureportFileDAO.insertReportFile(ureportFile);
        } else {
            ureportFile.setContent(content);
            ureportFile.setUpdateTime(currentDate);
            ureportFileDAO.updateReportFile(ureportFile);
        }
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * 获取没有前缀的文件名
     *
     * @param name
     * @return
     */
    private String getCorrectName(String name) {
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length(), name.length());
        }
        return name;
    }
}
